<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="page-title">设置</div>
          <div class="placeholder"></div>
        </div>
      </div>

      <!-- 设置列表 -->
      <div class="settings-list">
        <!-- 第一组：个人资料、二级密码 -->
        <div class="settings-group">
          <div class="settings-item" @click="handleProfile">
            <div class="item-left">
              <span class="item-text">个人资料</span>
            </div>
            <Icon icon="mdi:chevron-right" class="item-arrow" />
          </div>

          <div class="settings-item" @click="handleSecondaryPassword">
            <div class="item-left">
              <span class="item-text">二级密码</span>
            </div>
            <Icon icon="mdi:chevron-right" class="item-arrow" />
          </div>
        </div>

        <!-- 第二组：消息推送 -->
        <div class="settings-group">
          <div class="settings-item">
            <div class="item-left">
              <span class="item-text">消息推送</span>
            </div>
            <div class="switch-wrapper">
              <input
                type="checkbox"
                class="switch-input"
                :checked="notificationEnabled"
                @change="toggleNotification"
              >
              <span class="switch-slider"></span>
            </div>
          </div>
        </div>

        <!-- 底部按钮组 -->
        <div class="button-group">
          <button class="switch-account-btn" @click="handleSwitchAccount">
            切换账号
          </button>
          <button class="logout-btn" @click="handleLogout">
            退出登录
          </button>
        </div>
      </div>
      <!-- 二级密码弹窗 -->
      <div v-if="showPasswordDialog" class="custom-dialog-mask">
        <div class="custom-dialog">
          <div class="dialog-header">
            <h3>{{ hasSecondaryPassword ? '修改二级密码' : '设置二级密码' }}</h3>
            <span class="close-btn" @click="showPasswordDialog = false">×</span>
          </div>
          <div class="dialog-body">
            <div v-if="hasSecondaryPassword" class="input-group">
              <label class="input-label">原二级密码</label>
              <div class="password-input-wrapper">
                <input
                  v-model="oldPassword"
                  :type="showOldPassword ? 'text' : 'password'"
                  class="custom-input"
                  placeholder="请输入原二级密码"
                >
                <div class="password-toggle" @click="showOldPassword = !showOldPassword">
                  <Icon :icon="showOldPassword ? 'mdi:eye-off' : 'mdi:eye'"/>
                </div>
              </div>
              <div class="forgot-password" @click="handleForgotPassword">
                忘记原密码？
              </div>
            </div>

            <div class="input-group">
              <label class="input-label">新二级密码</label>
              <div class="password-input-wrapper">
                <input
                  v-model="newPassword"
                  :type="showNewPassword ? 'text' : 'password'"
                  class="custom-input"
                  :placeholder="hasSecondaryPassword ? '请输入新二级密码' : '请设置二级密码'"
                >
                <div class="password-toggle" @click="showNewPassword = !showNewPassword">
                  <Icon :icon="showNewPassword ? 'mdi:eye-off' : 'mdi:eye'"/>
                </div>
              </div>
            </div>

            <div class="input-group">
              <label class="input-label">确认新密码</label>
              <div class="password-input-wrapper">
                <input
                  v-model="confirmPassword"
                  :type="showConfirmPassword ? 'text' : 'password'"
                  class="custom-input"
                  placeholder="请再次输入新密码"
                >
                <div class="password-toggle" @click="showConfirmPassword = !showConfirmPassword">
                  <Icon :icon="showConfirmPassword ? 'mdi:eye-off' : 'mdi:eye'"/>
                </div>
              </div>
            </div>

            <button class="convert-confirm-btn" @click="handlePasswordSubmit">
              {{ hasSecondaryPassword ? '修改密码' : '设置密码' }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'SettingsPage',
  components: {
    Icon
  },
  data() {
    return {
      showPasswordDialog: false,
      hasSecondaryPassword: false,
      notificationEnabled: true,
      oldPassword: '',
      newPassword: '',
      confirmPassword: '',
      showOldPassword: false,
      showNewPassword: false,
      showConfirmPassword: false
    }
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },

    handleProfile() {
      this.$router.push('/userdistribution/settings/userinfo')
    },

    handleSecondaryPassword() {
      this.showPasswordDialog = true
    },

    handleSwitchAccount() {
      alert('切换账号')
      this.$router.push('/userdistribution/login')
    },

    handleLogout() {
      if(confirm('确定要退出登录吗？')) {
        alert('退出登录成功')
        this.$router.push('/userdistribution/login')
      }
    },

    toggleNotification() {
      this.notificationEnabled = !this.notificationEnabled
      alert(this.notificationEnabled ? '消息推送已开启' : '消息推送已关闭')
    },

    handlePasswordSubmit() {
      if (this.hasSecondaryPassword && !this.oldPassword) {
        alert('请输入原二级密码')
        return
      }

      if (!this.newPassword) {
        alert('请输入新二级密码')
        return
      }

      if (this.newPassword.length < 6) {
        alert('密码长度不能少于6位')
        return
      }

      if (this.newPassword !== this.confirmPassword) {
        alert('两次输入的密码不一致')
        return
      }

      console.log('设置二级密码:', {
        oldPassword: this.oldPassword,
        newPassword: this.newPassword
      })

      this.showPasswordDialog = false
      this.resetPasswordForm()
    },

    handleForgotPassword() {
      alert('请联系客服重置二级密码')
    },

    resetPasswordForm() {
      this.oldPassword = ''
      this.newPassword = ''
      this.confirmPassword = ''
      this.showOldPassword = false
      this.showNewPassword = false
      this.showConfirmPassword = false
    }
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.back-icon:hover {
  background: #f5f5f5;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.placeholder {
  width: 36px;
}

.settings-list {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.settings-group {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
  overflow: hidden;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 1px solid #f8f9fa;
}

.settings-item:last-child {
  border-bottom: none;
}

.settings-item:hover {
  background: #f8f9fa;
}

.item-left {
  display: flex;
  align-items: center;
}

.item-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.item-arrow {
  color: #999;
  font-size: 18px;
}

.switch-wrapper {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.switch-input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 24px;
}

.switch-slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

.switch-input:checked + .switch-slider {
  background-color: #1890ff;
}

.switch-input:checked + .switch-slider:before {
  transform: translateX(20px);
}

.button-group {
  margin-top: auto;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.switch-account-btn, .logout-btn {
  width: 100%;
  height: 48px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.switch-account-btn {
  background: #f0f0f0;
  color: #333;
}

.switch-account-btn:hover {
  background: #e0e0e0;
  transform: translateY(-1px);
}

.logout-btn {
  background: #ff4d4f;
  color: white;
}

.logout-btn:hover {
  background: #ff7875;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
}

.custom-dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  border-radius: 20px;
}

.custom-dialog {
  width: 280px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  font-size: 20px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.close-btn:hover {
  color: #666;
}

.dialog-body {
  padding: 20px;
}

.input-group {
  margin-bottom: 20px;
  position: relative;
}

.input-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.password-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.password-input-wrapper .custom-input {
  padding-right: 40px;
}

.password-toggle {
  position: absolute;
  right: 12px;
  color: #666;
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
}

.custom-input {
  width: 100%;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  background: white;
}

.custom-input:focus {
  border-color: #1890ff;
  background: white;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.custom-input::placeholder {
  color: #999;
}

.forgot-password {
  font-size: 12px;
  color: #1890ff;
  cursor: pointer;
  text-align: right;
  margin-top: 8px;
}

.forgot-password:hover {
  text-decoration: underline;
}

.convert-confirm-btn {
  width: 100%;
  height: 44px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-top: 8px;
  transition: background-color 0.3s;
}

.convert-confirm-btn:hover {
  background: #40a9ff;
}

.convert-confirm-btn:active {
  background: #096dd9;
}

@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }
}
</style>
